<template>
	<view>
		<!-- <uni-nav-bar left-icon="back" left-text="" right-text="清空" @clickRight='clear' title='购物车'></uni-nav-bar> -->

		<view class="item" v-for="(item,index) in datalist" :key="index">
			<view class="top">
				<view class="left">

					<checkbox-group @change="checkboxChange(item.id)" name="limitarea">
						<checkbox class="checkbox" :value="item.value" :checked="item.check" />
					</checkbox-group>

					<image :src="item.productPic" mode=""></image>
				</view>
				<view @click="togoodsinfo(item.productId)" class="right">
					<view class="title">
						{{item.productName}}
					</view>
					<view class="">编号: {{item.productSkuId}}</view>
					<view class="">价格: {{item.price}}</view>
					<view class="">数量: {{item.quantity}}</view>
				</view>
			</view>
			<view class="bottom">
				<view @click="low(item.id)" class="low">
					-
				</view>
				<view class="count">
					{{item.quantity}}
				</view>
				<view @click="add(item.id)" class="add">
					+
				</view>
			</view>
		</view>

		<view class="bottom-tool">
			<view class="check-left">
				<text>全选</text>
				<checkbox-group @change="allChange">
					<checkbox class="checkbox" :checked="all" />
				</checkbox-group>
			</view>
			<view class="right-pay">
				<text class="total">合计:￥: <text class="money">{{total}}</text></text>
				<view @click="payfor" class="btn">
					结算
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import datalist from "../../../data/city.js"
	import {
		findAllCartItem,
		addCart,
		clearCart,
		delCartItems
	} from "../../../api/my/car/car.js"
	import {
		addPreOrder,
		getPreOrderById,
		addConfirmOrder,
		payConfirm,
		receiveDone,
		deleteOrder
	} from "../../../api/my/order/order.js"
	export default {
		data() {
			return {
				all: false,
				total: 0,
				datalist: '',
			};
		},
		onLoad() {
			findAllCartItem({}).then(res => {
				this.datalist = res.data.items
				console.log(res);
				if (res.success) {
					this.datalist.forEach(el => {
						el.check = false
					})
				}
			})
		},
		methods: {
			// 到商品详细页面
			togoodsinfo(id) {
				console.log(id);
				uni.navigateTo({
					url: `../../home/goodsinfo/goodsinfo?id=${id}`
				})
			},
			// 清空购物车
			clear() {
				var _this = this
				uni.showModal({
					title: '确定清空购物车么',
					success: function(res) {
						if (res.confirm) {
							clearCart().then(res => {
								console.log(res);
								if (res.success) {
									uni.showToast({
										title: '清空购物车成功'
									})
									_this.datalist = ''
								}
							})
						} else if (res.cancel) {
							uni.showToast({
								title: '取消了操作'
							})

						}
					}
				})
			},
			// 付款
			payfor() {
				var isclick = this.datalist.filter(el => {
					return el.check == true
				})
				console.log(isclick);
				if (isclick) {
					var orderItemList = [];
					var obj = {}
					isclick.forEach(el => {
						obj.cartId = el.id;
						obj.productId = el.productId;
						obj.productQuantity = el.quantity;
						obj.productSkuId = el.productSkuId
						orderItemList.push(obj)
						obj = {}
					})
					console.log(orderItemList);

					addPreOrder({
						"orderItemList": orderItemList
					}).then(res => {
						console.log(res);
						var ids = res.data.orderId
						if (res.success) {
							uni.showToast({
								title: "跳转至付款页面"
							})
							setTimeout(function() {
								uni.navigateTo({
									url: `../waitpay/waitpay?id=${ids}`
								})
							}, 1700)
						}
					})



				} else {
					uni.showToast({
						title: '至少选择一款商品',
						icon: 'none'
					})
				}
			},
			// 计算价格
			init() {
				this.total = this.datalist.reduce((pre, next) => {
					if (next.check) {
						return pre + next.quantity * next.price
					} else {
						return pre
					}
				}, 0)

			},
			// 全选和反选
			allChange() {
				if (this.all) {
					this.all = false
					this.datalist.forEach(el => {
						el.check = false
					})
					this.total = 0
					this.init()
				} else {
					this.all = true
					this.datalist.forEach(el => {
						el.check = true
					})
					this.init()
				}
			},
			// 单个选择
			checkboxChange(ids) {
				var index = this.datalist.findIndex(el => {
					return el.id == ids
				})

				if (this.datalist[index].check) {
					this.datalist[index].check = false
				} else {
					this.datalist[index].check = true
				}
				this.all = this.datalist.every(el => {
					return el.check == true
				})
				this.init()
			},
			//数量减
			low(idss) {
				console.log(idss);
				var index = this.datalist.findIndex(el => {
					return el.id == idss
				})
				var arr = []
				arr.push(idss)
				if (this.datalist[index].quantity == 1) {
					var _this = this
					uni.showModal({
						title: '确定删除此商品么',
						success: function(res) {
							if (res.confirm) {
								delCartItems(arr).then(res => {
									console.log(res);
									uni.showToast({
										title:'删除成功'
									})
									if (res.success) {
										_this.datalist.splice(index, 1)
									}
								})

							} else if (res.cancel) {
								return
							}
						}
					})
				} else {
					this.datalist[index].quantity--
				}

				this.init()
			},
			//数量加
			add(ids) {
				var index = this.datalist.findIndex(el => {
					return el.id == ids
				})
				this.datalist[index].quantity++
				this.init()
			}
		},
		computed: {

		}
	}
</script>

<style lang="scss">
	.item {
		width: 710rpx;
		padding: 20rpx 40rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		overflow: hidden;
		margin: 20rpx auto;
		box-sizing: border-box;

		.top {
			display: flex;
			justify-content: space-between;
			box-sizing: border-box;

			.left {
				width: 200rpx;
				position: relative;
				box-sizing: border-box;


				.checkbox {
					position: absolute;
					left: 0;
					box-sizing: border-box;
					top: 40rpx;
				}

				image {
					width: 132rpx;
					height: 132rpx;
					border-radius: 12rpx;
					margin-left: 70rpx;
					box-sizing: border-box;
				}
			}

			.right {
				width: 400rpx;
				font-size: 22rpx;
				box-sizing: border-box;
				color: #8d8d8d;

				.title {
					font-size: 26rpx;
					color: #3e3e3e;
					box-sizing: border-box;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}

		}

		.bottom {
			width: 260rpx;
			float: right;
			display: flex;
			justify-content: space-between;
			box-sizing: border-box;

			.low,
			.add {
				width: 50rpx;
				height: 50rpx;
				background-color: #F1ECE7;
				text-align: center;
				line-height: 42rpx;
				box-sizing: border-box;
				font-size: 40rpx;
				border-radius: 50%;
			}

			.count {
				width: 120rpx;
				height: 50rpx;
				background-color: #F1ECE7;
				text-align: center;
				line-height: 50rpx;
				font-size: 34rpx;
				box-sizing: border-box;
				border-radius: 30rpx;
			}
		}
	}

	.bottom-tool {
		position: fixed;
		bottom: 0;
		background-color: #FFFFFF;
		width: 100%;
		box-sizing: border-box;
		height: 150rpx;
		display: flex;
		justify-content: space-between;
		padding: 0 40rpx;

		.check-left {
			line-height: 150rpx;
			font-size: 24rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;

			text {
				margin-right: 10rpx;
				box-sizing: border-box;
			}
		}

		.right-pay {
			display: flex;
			justify-content: space-between;
			box-sizing: border-box;

			.total {
				font-size: 28rpx;
				box-sizing: border-box;
				line-height: 150rpx;
				margin-right: 10rpx;

				.money {
					color: red;
					box-sizing: border-box;
				}

			}

			.btn {
				width: 208rpx;
				height: 80rpx;
				background: #354e44;
				border-radius: 12rpx;
				color: #fff;
				box-sizing: border-box;
				font-size: 36rpx;
				line-height: 80rpx;
				text-align: center;
				margin-top: 35rpx;
			}
		}
	}
</style>
